package controllers

import (
	"xmgl/models"

	"log"
	"net/http"

	"github.com/astaxie/beego"
)

type ZhjscgjlController struct {
	beego.Controller
}

type Api_cgjlController struct {
	beego.Controller
}

func init() {

}

func (this *ZhjscgjlController) Get() {
	var (
		menu models.Menu_gl
		user models.Sut_user
	)

	if this.GetSession("USER") == nil {
		//没有取到session值，页面跳转
		http.Redirect(this.Ctx.ResponseWriter, this.Ctx.Request, "/login", http.StatusFound)
	}
	tmp, ok := this.GetSession("USER").(models.Sut_user) //注意类型断言
	if ok {
		user = tmp
	}

	this.Data["WEBSITE"] = "房间设备管理"
	this.Data["MENU_LEFT"] = menu.Get_left_menu()
	this.Data["MENU_RIGHT"] = menu.Get_right_menu()
	this.Data["USER_NAME"] = user.User_name

	this.Layout = "layout.html"
	this.TplName = "zhjscgjl.html"
	this.LayoutSections = make(map[string]string)
	this.LayoutSections["HtmlHeader"] = "html_header.html"
}

//添加参观记录
func (this *ZhjscgjlController) Post() {
	var (
		z   models.Sut_zhjscgjl
		err error
	)
	//不能取到数据：json.Unmarshal(this.Ctx.Input.RequestBody, &z)
	/*不能取到数据：
	if err := this.ParseForm(&z); err != nil {
		//handle error
		this.Data["json"] = err //当没有错误时，返回AJAX为空
		this.ServeJSON()        //输出
		return
	}*/
	/*不能用this.ParseForm(&z)直接解析到 struct*/
	z.Riqi = this.GetString("riqi", "")
	z.Xxmc = this.GetString("xxmc", "")
	z.Lxr = this.GetString("lxr", "")
	z.Tel = this.GetString("tel", "")
	z.Rs, _ = this.GetInt("rs", 0)
	z.Zhiw = this.GetString("zhiw")
	z.Xxjdr = this.GetString("xxjdr", "")
	z.Jsr = ""
	_, err = z.Add()
	this.Data["json"] = err //当没有错误时，返回AJAX为空
	this.ServeJSON()        //输出
	log.Println("riqi:", z.Riqi, ", xxmc:", z.Xxmc, ", lxr:", z.Lxr, ", tel:", z.Tel, ", rs:", z.Rs, ", zhiw:", z.Zhiw, ", xxjdr:", z.Xxjdr, ", jsr:", z.Jsr)
	return
}

//Api调用，取得参观记录列表
func (this *Api_cgjlController) Get() { //Api_cgjlController
	this.Data["json"], _ = models.GetZhjsAlls()
	this.ServeJSON()
}

/*单独显示一个页面，不需要认证，显示智慧教室的参观记录*/
type ZhjscgController struct {
	beego.Controller
}

func (this *ZhjscgController) Get() {
	var ()
	/*不进行用户登陆验证
	if this.GetSession("USER") == nil {
		//没有取到session值，页面跳转
		http.Redirect(this.Ctx.ResponseWriter, this.Ctx.Request, "/login", http.StatusFound)
	}
	tmp, ok := this.GetSession("USER").(models.User_struct) //注意类型断言
	if ok {
		user = tmp
	}
	*/
	this.Data["Website"] = "智慧教室参观记录表"
	this.TplName = "zhjscg.html"
}
